/**
 * Created by hwx on 2016/4/22.
 */
"use strict";

var { Point } = require("../Point.js");
var { Plot } = require("../Plot.js");

class NodeSet {
  static speed = 0.5

  /**
   * @param {Number} len
   */
  constructor(len) {
    this.pointWH = new Point(window.innerWidth, window.innerHeight);
    /**
     * @type {Point[]}
     */
    this.nodeArr = new Array(len);
    this.randInit();
  }
  randInit(len = this.nodeArr.length, color) {
    for (var i = 0; i < len; i++) {
      this.nodeArr[i] = new Point(
        (Math.random() - 0.5) * this.pointWH.x,
        (Math.random() - 0.5) * this.pointWH.y
      );
    }
    return this.nodeArr;
  }

  /**
   * 获取选中点,返回索引
   * @param {Point[]} nodeArr
   * @param {Point} point
   */
  static getSelect(nodeArr, point) {
    //自己验证 point 节约内存开销
    var lenAll = [];
    for (var i = 0; i < nodeArr.length; i++) {
      lenAll[i] = Point.len(nodeArr[i], point);
    }
    return lenAll.indexOf(Math.min(...lenAll));
  }


  trans() {
    //随机运动
    for (var i = 0; i < this.nodeArr.length; i++) {
      this.nodeArr[i].x += (Math.random() - 0.5) * NodeSet.speed;
      this.nodeArr[i].y += (Math.random() - 0.5) * NodeSet.speed;
    }
  }
  trans2() {
    this.getlen = function (i) {
      var sum_len4 = 0;
      for (var j = 0; j < 4; j++) {
        sum_len4 += Point.len(this.nodeArr[i], this.nodeArr[connect_Arr[i][j]]); //数字指针
      }
      return sum_len4;
    };
    for (var i = 0; i < this.nodeArr.length; i++) {
      var obj = { x: this.nodeArr[i].x, y: this.nodeArr[i].y };
      var len1 = this.getlen(i);
      this.nodeArr[i].x += (Math.random() - 0.5) * NodeSet.speed;
      this.nodeArr[i].y += (Math.random() - 0.5) * NodeSet.speed;
      var len2 = this.getlen(i);
      
      if (len2 > len1) this.nodeArr[i] = obj;
    }
  }
  /**
   * 通过一个js函数创建图像;
   * @param {*} func 
   */
  initByFunc(func) {
    var d = this.pointWH.x / this.nodeArr.length;
    for (let i1 = 0; i1 < this.nodeArr.length; i1++) {
      this.nodeArr[i1].x = (-this.pointWH.x / 2 + i1 * d);
      this.nodeArr[i1].y = func(this.nodeArr[i1].x);
    }
  }
}

module.exports = NodeSet